<?php
namespace app\api\service;
use think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/26 0026
 * Time: 15:40
 */

class Index extends Controller {

    /**
     * 获取首页数据
     * @return mixed
     */
    public static function get_index_data() {
        if (!$data = cache('index_data')) {
            $banner = model('Banner')->getList(['status' => 1]);
            foreach ($banner as $key => $val) {
                $banner[$key]['image'] = get_img_url($val['image']);
            }

            $index_data = model('system')->get(['key' => 'index_page']);
            $index_data = json_decode($index_data['value'], true);
            $model = model('Product');
            foreach ($index_data as $key => $val) {
                $product = $model->where('id', 'in', $val['ids'])->where('delete', 0)->where('status', 2)->select();
                foreach ($product as $k => $v) {
                    $product[$k]['thumb'] = get_img_url($v['thumb']);
                }
                $data[$key]['product'] = $product;
                $data[$key]['image'] = isset($val['image']) ? get_img_url($val['image']) : '';
            }
            $data['banner'] = $banner;

            cache('index_data', $data);
        }
        return api_return('200', $data);
    }
}